<?php
namespace  app\common;

use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Controller;

class  Base extends Controller {
     const  OK = 2001;                  //成功
     const  FAIL = 2000;                //失败
     const PARAMETER_ERROR= 2002;       //参数缺失
     const NOT_LOGGED_IN= 2003;         //未登入

     const JUMP_PAGE= 2100;             //跳转页面
     const POPUP= 2200;                 //弹窗
     const JURISDICTION_ERROR= 2300;    //权限错误
     const NOT_FIND= 2400;              //查找无结果

    static protected $page = 1;         //当前页码
    static protected $pageSize =  5;    //当前条数

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->setPage();
    }
//获取分页信息
    public function setPage(){
        self::$page = $this->request->post('page',1);                   //当前的分页
        self::$pageSize = $this->request->post('page_size',5);  //当前分页数据条数
    }

    /**
     * @makeToken   生成token
     * @param $userId
     * @param $phone
     * @return string
     */
    public function makeToken($id,$phone){
        $str = md5($id.time().$phone);
        return $str;
    }
    /**
     * @commentTime 评论时间
     * @param $time
     * @return string
     */
    public function commentTime($time){
        $remaining = time() - $time;
        if ($remaining > 31104000){
            return floor($remaining / 31104000).'年前';
        }
        if ($remaining > 2592000){
            return floor($remaining / 2592000).'月前';
        }
        if ($remaining >= 86400){
            return floor($remaining / 86400).'天前';
        }
        if ($remaining >= 3600){
            return floor($remaining / 3600).'时前';
        }
        if ($remaining >= 60){
            return floor($remaining / 60).'分前';
        }
        return $remaining.'秒前';
    }

    /**
     * @assoc_unique    二维数组去重
     * @param $arr
     * @param $key
     * @return mixed
     */
    public function assoc_unique($arr, $key){
        $tmp_arr = array();
        foreach ($arr as $k => $v) {
            if (in_array($v[$key], $tmp_arr)) {//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
                unset($arr[$k]);
            } else {
                $tmp_arr[] = $v[$key];
            }
        }
        return $arr;
    }
}